﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Validation;
using XStudio.XCRF.Data.Enums;
using XStudio.XCRF.Data.Entities;

namespace XStudio.XCRF.Data.Business.Validate
{
    /// <summary>
    /// 成员业务验证属性
    /// </summary>
    public class MemberValidate : ValidateAttribute
    {
        public override IEnumerable<DbValidationError> ValidateAdd(DbEntityEntry entityEntry, XCRFContext context)
        {
            List<DbValidationError> errors = new List<DbValidationError>();
            Member member = (Member)entityEntry.Entity;
            Member currentMember = context.CurrentMember;
            switch ((MemberTypes)currentMember.Type)
            {
                case MemberTypes.Administrator: break;
                case MemberTypes.Owner:
                    if (member.HospitalID != currentMember.HospitalID)
                        errors.Add(new DbValidationError("HospitalID", "医院主管只能为自己的医院添加用户"));
                    break;
                case MemberTypes.Researcher:
                    errors.Add(new DbValidationError(null, "研究者不能添加新用户"));
                    break;
            }
            if (context.Members.Exist(member.LoginName))
                errors.Add(new DbValidationError("LoginName", "登录名重复"));
            return errors;
        }

        public override IEnumerable<DbValidationError> ValidateEdit(DbEntityEntry entityEntry, XCRFContext context)
        {
            List<DbValidationError> errors = new List<DbValidationError>();
            Member member = (Member)entityEntry.Entity;
            Member currentMember = context.CurrentMember;
            switch ((MemberTypes)currentMember.Type)
            {
                case MemberTypes.Administrator: break;
                case MemberTypes.Owner:
                    if (currentMember.HospitalID != member.HospitalID)
                        errors.Add(new DbValidationError("HospitalID", "医院主管只能更新自己医院成员的信息"));
                    break;
                case MemberTypes.Researcher:
                    if (member.ID != currentMember.ID)
                        errors.Add(new DbValidationError("ResearcherID", "研究者只能更新自己的信息"));
                    break;
            }
            return errors;
        }

        public override IEnumerable<DbValidationError> ValidateDelete(DbEntityEntry entityEntry, XCRFContext context)
        {
            List<DbValidationError> errors = new List<DbValidationError>();
            Member currentMember = context.CurrentMember;
            Member member = (Member)entityEntry.Entity;
            if (member.ID == currentMember.ID)
                errors.Add(new DbValidationError("ID", "不能删除自己"));
            switch ((MemberTypes)currentMember.Type)
            {
                case MemberTypes.Administrator:
                    break;
                case MemberTypes.Owner:
                    if (member.HospitalID != currentMember.HospitalID)
                        errors.Add(new DbValidationError("HospitalID", "医院主管只能删除自己医院的人员"));
                    break;
                case MemberTypes.Researcher:
                    errors.Add(new DbValidationError(null, "研究者无权限删除任何用户"));
                    break;
            }
            return errors;
        }
    }
}
